package com.d3works.apps.base.rest.http;

import com.d3works.apps.base.rest.common.JS;
import com.d3works.apps.base.rest.common.R;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;

public class RequestEventArgs {

  private final HttpURLConnection _conn;

  private String method;

  public String getMethod() {
    return method;
  }

  public RequestEventArgs(HttpURLConnection conn, String method) {
    this._conn = conn;
    this.method = method;
  }

  public void setHeader(String key, String value) {
    _conn.setRequestProperty(key, value);
  }

  public void setHeaders(InputStream input) throws IOException {
    Properties props = new Properties();
    props.load(new InputStreamReader(input, "utf-8"));
    input.close();
    setHeaders(props);
  }

  public void setHeaders(Properties props) {
    Set<String> keys = props.stringPropertyNames();
    for (String key : keys) {
      String value = props.getProperty(key);
      if (StringUtils.isNotEmpty(value)) {
        _conn.setRequestProperty(key, value);
      }
    }
  }

  public void writeRequestBody(Object in) throws IOException {
    OutputStream bodyOutput = null;
    InputStream input = null;
    try {
      bodyOutput = _conn.getOutputStream();
      if ((input = getBodyInputStream(in)) != null) {
        R.bridgeWrite(input, bodyOutput);
      }
    } finally {
      if (input != null) {
        input.close();
        input = null;
      }
      if (bodyOutput != null) {
        bodyOutput.flush();
        bodyOutput.close();
        bodyOutput = null;
      }
    }
  }

  public OutputStream getRequestOut() throws IOException {
    return _conn.getOutputStream();
  }

  public void asGet() throws ProtocolException {
    _conn.setRequestMethod("GET");
  }

  public void setMethod(String method) throws ProtocolException {
    _conn.setRequestMethod("GET");
  }

  /**
   * Json数据提交
   *
   * @throws ProtocolException
   */
  public void jsonPost() throws ProtocolException {
    _conn.setRequestMethod("POST");
    _conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  }

  public void jsonPost(Object body) throws ProtocolException, IOException {
    _conn.setRequestMethod("POST");
    _conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    writeRequestBody(body);
  }

  /**
   * 表单提交
   *
   * @throws ProtocolException
   */
  public void formPost() throws ProtocolException {
    _conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
    _conn.setRequestMethod("POST");
  }

  public void method(String method) throws ProtocolException {
    _conn.setRequestMethod(method);
  }

  public InputStream getBodyInputStream(Object body) throws IOException {
    if (body == null) {
      return null;
    } else if (body instanceof String) {
      return new ByteArrayInputStream(((String) body).getBytes("UTF-8"));
    } else if (body instanceof InputStream) {
      return (InputStream) body;
    } else {
      String json = JS.toJson(body);
      return new ByteArrayInputStream(json.getBytes("UTF-8"));
    }
  }
}
